package MYSQLConection;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pablo
 */
public class DatabaseManager {

    private Connection connection;
    private Savepoint svpt;
    String driver = "com.mysql.jdbc.Driver";
    String url = "jdbc:mysql://localhost/agenda";
    String userDB = "root";
    String passDB = "jorge";


		//crea la conexion
	public DatabaseManager()
	{
		try {
                    //String url = "jdbc:postgresql://127.0.0.1:5432/Examen?user=postgres&password=cristinapablito";
                    Class.forName(driver);
                    connection = DriverManager.getConnection(url, userDB, passDB);
                    //cominte permite guardar cambios
                    //rollback regresa al ultimo moneto 
                    connection.setAutoCommit(false);// cada sentecia se guarda en la base de datos  yo le digo en q momento guarda con el false
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException ex) {
            Logger.getLogger(DatabaseManager.class.getName()).log(Level.SEVERE, null, ex);
        }
	}

	public void commit()
	{
		try
		{
			connection.commit();
			svpt = connection.setSavepoint();
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
	}

	public Connection getConnection()
	{
		return connection;
	}
   ///coneciones paralelas 
	public void openConnectionTo(String url)
	{

		try
		{			
			connection = DriverManager.getConnection(url, userDB, passDB);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}

	}
         //borreme todo lo que esta hecho
	public void rollBack()
	{
		try
		{
			if (svpt != null)
			{
				connection.rollback(svpt);
				connection.commit();
			}
		} catch (SQLException e)
		{
			// TODO Errors?
			e.printStackTrace();
		}
	}
    
}
